package com.justcan.demo.cartoon.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import com.justcan.demo.cartoon.R;
import com.justcan.demo.cartoon.utils.anim.AnimObject;


public class FogObject extends AnimObject {

	/**
	 * 宽高
	 */
	private float mWidth = 0;
	private float mHeight = 0;
	/**
	 * 坐标
	 */
	private int mX = 0;
	private int mY = 0;
	/**
	 * 是否已经初始化
	 */
	private boolean mHasInit = false;
	/**
	 * 图
	 */
	private Bitmap mBitmap = null;
	/**
	 * 绘制Rect
	 */
	private Rect mSrcRect = null;
	private Rect mDstRect = null;
	/**
	 * Paint
	 */
	private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);

	public FogObject(View mRootAnimView, Context mContext) {
		super(mRootAnimView, mContext);
		if (getAnimSceneWidth() != 0 && getAnimSceneHeight() != 0) {
			init();
		}
	}

	@Override
	public void onSizeChange(int w, int h) {
		super.onSizeChange(w, h);
		init();
	}

	/**
	 * 初始化
	 */
	private void init() {
		if (!mHasInit) {
			mBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.fog);
			int mSceneWidth = getAnimSceneWidth();
			int mSceneHeight = getAnimSceneHeight();
			mWidth = mSceneWidth;	// 宽度
			mHeight = mWidth * 0.2875f;		// 根据图片计算合理的高度
			mX = 0;			// 定义初始位置
			mY = (int) (mSceneHeight - mHeight);
			mSrcRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
			mDstRect = new Rect(mX, mY, (int) (mWidth + mX), (int) (mHeight + mY));
			mHasInit = true;
		}
	}

	@Override
	public void draw(Canvas canvas, int sceneWidth, int sceneHeight) {
		if (mHasInit) {
			canvas.drawBitmap(mBitmap, mSrcRect, mDstRect, mPaint);
		}
	}

	/**
	 * 获取位置
	 */
	public Rect getFogRect() {
		return mDstRect;
	}
}
